import { validateCardType } from '@/utils/cardUtils';
import { getNextPlayerId } from '@/utils/common.js';
import { canBeat } from '@/utils/cardComparison';

export function useGameActions(player, gameState) {
  // 处理出牌事件
  const handleCardPlayed = (payload) => {
    const currentCard = payload.card;
    const index = gameState.selectedCard.value.findIndex(
      c => c.uniqueId === currentCard.uniqueId
    );

    if (index > -1) {
      gameState.selectedCard.value.splice(index, 1);
    } else {
      gameState.selectedCard.value.push(currentCard);
    }
  };
  
  // 出牌逻辑
  const playCard = (sendMessage, showWinDialog) => {
    const selectedCards = gameState.selectedCard.value;
    console.log(selectedCards)
    if (selectedCards.length === 0) {
      console.warn('请选择要出的牌');
      return false;
    }
    
    const cardType = validateCardType(selectedCards);
    console.log("类型")
    console.log(cardType)
    if (!cardType) {
      console.warn('牌型不符合斗地主规则');
      return false;
    }
    
    if (gameState.upCard.value!=null&& gameState.upCard.value.length > 0) {
      console.log(selectedCards+" 99 "+ gameState.upCard.value)
      if (!canBeat(selectedCards, gameState.upCard.value)) {
        console.warn('您出的牌不能压过上家');
        return false;
      }
    }
    
    // 更新玩家手牌
    const removeKeys = new Set(selectedCards.map(card => `${card.rank}${card.suit.log}`));
    gameState.pais.value = gameState.pais.value.filter(
      pai => !removeKeys.has(`${pai.rank}${pai.suit.log}`)
    );
    
    // 如果牌出完了，显示胜利对话框
    if (gameState.pais.value.length === 0) {
      showWinDialog.value = true;
      sendMessage({
        messageType: "win",
        roomId: gameState.Room.value.roomId,
        player: player.value,
        winnerId: player.value.id
      });
    }
    
    // 准备发送给下一位玩家
    const nextPlayerId = getNextPlayerId(player.value.id, gameState.Room.value.players);
    console.log("下一个人id"+nextPlayerId)
    sendMessage( {
      messageType:"playcard",
      roomId: gameState.Room.value.roomId,
      player: player.value,
      nextplayerid: nextPlayerId,
      pai: selectedCards,
    });
    
    return true;
  };
  
  // 不出牌
  const passCard = (sendMessage) => {
    const nextPlayerId = getNextPlayerId(player.value.id, gameState.Room.value.players);
    
    sendMessage( {
      messageType:"playcard",
      roomId: gameState.Room.value.roomId,
      player: player.value,
      nextplayerid: nextPlayerId,
      pai: [],
    });
    
    return true;
  };
  
  // 叫地主
  const callLandlord = (score, sendMessage) => {
    console.log(gameState.pais)
    console.log(gameState.Room.value.players)

    const nextPlayerId = getNextPlayerId(player.value.id, gameState.Room.value.players);
    console.log("下一个人id"+nextPlayerId)
    return sendMessage( {
      messageType:"calland",
      roomId: gameState.Room.value.roomId,
      player:player.value,
      bid: true,
      score:player.value.score,
      nextplayerid: nextPlayerId
    });
  };
  
  // 不叫地主
  const passLandlord = (sendMessage) => {
    const nextPlayerId = getNextPlayerId(player.value.id, gameState.Room.value.players);
    
    return sendMessage( {
      messageType:"calland",
      roomId: gameState.Room.value.roomId,
      player:player.value,
      bid: false,
      score: 2,
      nextplayerid: nextPlayerId 
    });
  };
  
  return {
    handleCardPlayed,
    playCard,
    passCard,
    callLandlord,
    passLandlord
  };
}